package m.store.action;

import m.base.action.BaseStatusAction;
import m.base.util.LevelUtil;
import m.base.util.PowerUtil;
import m.common.action.ActionMeta;
import m.common.action.RequestMeta;
import m.common.action.VueResult;
import m.common.model.util.ModelQueryList;
import m.common.model.util.QueryCondition;
import m.html.VueFormResult;
import m.html.VueListResult;
import m.html.VueTabResult;
import m.html.element.attr.Align;
import m.html.element.attr.ButtonType;
import m.html.form.*;
import m.html.list.*;
import m.html.tab.TabItem;
import m.store.model.GuestInfo;
import m.store.service.GuestInfoService;
import m.store.util.ColorUtil;
import m.system.exception.MException;
import m.system.util.JSONMessage;
import m.system.util.StringUtil;

@ActionMeta(name = "store/guestInfo")
public class GuestInfoAction extends BaseStatusAction {
    private GuestInfo model;

    /**
     * 保存
     */
    @RequestMeta(logTitle = "基础数据",logDescription = "保存客户信息")
    public JSONMessage doSave() throws Exception {
//        PowerUtil.verifyPower(getStatusPower());
        model.setStore(PowerUtil.getSessionStore());
        String msg=getService(GuestInfoService.class).save(model);
        return new JSONMessage(0,msg)
                .push("model", model);
    }
    /*
     * 编辑客户列表
     */
    public VueResult toEdit() throws Exception {
        //PowerUtil.verifyPower(getStatusPower());//权限判断
        if(null!=model&&!StringUtil.isSpace(model.getOid())){
            model= ModelQueryList.getModel(model,"*","inputUser.realname","manageUser.realname");
            PowerUtil.verifyStoreData(model.getStore().getOid());
        }else{
            if(null==model){
                model=new GuestInfo();
                model.setType(GuestInfo.Type_Company);
            }
            model.setStore(PowerUtil.getSessionStore(true));
            model.setInputUser(PowerUtil.getSessionUser(true));
            model.setManageUser(PowerUtil.getSessionUser());
            model.setStockAmount(0d);
            model.setStockPayAmount(0d);
            model.setSaleAmount(0d);
            model.setSaleIncomeAmount(0d);
            model.setSurplusAmount(0d);
        }
        ModelForm form = new ModelForm("客户信息");
        form.addItem(
            FormInput.radio("类型", "model.type", "m.store.model.GuestInfo!type"),
            FormInput.input("客户名称", "model.name").setSpan(2).setPlaceholder("请输入名称"),
            FormInput.input("负责人", "model.dutyUser").setPlaceholder("请输入负责人"),
            FormInput.input("联系电话", "model.tel").setPlaceholder("请输入联系电话"),
            FormHtml.inputStyle("录入人","{{model.inputUser.realname}}"),
            FormInput.input("客户地址", "model.address").setSpan(2).setPlaceholder("请输入地址"),
            FormInput.selectOneAdmin("管理用户","model","manageUser","model.store.oid","''","{{model.manageUser.realname}}"),
            FormInput.textarea("描述", "model.description").setSpan(3).setPlaceholder("请输入描述"),
            FormHtml.inputStyle("入库金额","{{formatNumber(model.stockAmount,'#,##0.00')}}元 (已结{{formatNumber(model.stockPayAmount,'#,##0.00')}}元)"),
            FormHtml.inputStyle("出库金额","{{formatNumber(model.saleAmount,'#,##0.00')}}元 (已结{{formatNumber(model.saleIncomeAmount,'#,##0.00')}}元)"),
            FormHtml.inputStyle("剩余金额","{{formatNumber(model.surplusAmount,'#,##0.00')}}元")
        )
        .addData("model",model)
        .addButton(
            FormButton.pri("保存","doSave")
        )
        .addMethod(
            FormEvent.backFlag,
            FormEvent.doJson("doSave",actionUrl("doSave"),"{model:this.model}")
                .success("backFlag")
        );
        VueFormResult result=new VueFormResult();
        return result.addForm(form);
    }
    @RequestMeta(params = {"guestOid"})
    public VueResult toRecord(String guestOid){
        JSONMessage p=new JSONMessage();
        p.push("params",new JSONMessage().push("guestOid",guestOid));
        VueTabResult tab=new VueTabResult("商品记录");
        tab.addItems(new TabItem("stockRecord","入库记录",actionUrl(GoodsStockRecordAction.class,"goodsRecordList"),p));
        tab.addItems(new TabItem("payRecord","付款记录",actionUrl(PayAmountRecordAction.class,"recordList"),p));
        tab.addItems(new TabItem("outRecord","出库记录",actionUrl(GoodsOutRecordAction.class,"goodsRecordList"),p));
        tab.addItems(new TabItem("incomeRecord","收款记录",actionUrl(IncomeAmountRecordAction.class,"recordList"),p));
        return tab;
    }
    public VueResult toView() throws Exception {
        //PowerUtil.verifyPower(getStatusPower());//权限判断
        if(null!=model&&!StringUtil.isSpace(model.getOid())){
            model= ModelQueryList.getModel(model,"*","inputUser.realname","manageUser.realname");
        }else{
            throw new MException(this.getClass(),"参数错误");
        }
        ModelForm form = new ModelForm("客户信息");
        form.addItem(
            FormHtml.inputStyle("类型", "{{dict('m.store.model.GuestInfo!type',model.type)}}"),
            FormHtml.inputStyle("客户名称", "{{model.name}}").setSpan(2),
            FormHtml.inputStyle("负责人", "{{model.dutyUser}}"),
            FormHtml.inputStyle("联系电话", "{{model.tel}}"),
            FormHtml.inputStyle("录入人","{{model.inputUser.realname}}"),
            FormHtml.inputStyle("客户地址", "{{model.address}}").setSpan(2),
            FormHtml.inputStyle("管理用户","{{model.manageUser.realname}}"),
            FormHtml.inputStyle("描述", "{{model.description}}").setSpan(3),
            FormHtml.inputStyle("入库金额","{{formatNumber(model.stockAmount,'#,##0.00')}}元 (已结{{formatNumber(model.stockPayAmount,'#,##0.00')}}元)"),
            FormHtml.inputStyle("出库金额","{{formatNumber(model.saleAmount,'#,##0.00')}}元 (已结{{formatNumber(model.saleIncomeAmount,'#,##0.00')}}元)"),
            FormHtml.inputStyle("剩余金额","{{formatNumber(model.surplusAmount,'#,##0.00')}}元")
        )
        .addData("model",model);
        LoadPage form1=new LoadPage("guestRecordTab",actionUrl("toRecord"),"{guestOid:this.model.oid}");
        form1.setMounted("this.guestRecordTab();");
        VueFormResult result=new VueFormResult();
        return result.addForm(form).addForm(form1);
    }
    private ListColumn getTypeColumn(){
        String dict="m.store.model.GuestInfo!type";
        return toElementColumn("类型", "type", 100,
                ListElement.tag(dict, ColorUtil.getGuestType(),false)
        ).setAlign(Align.CENTER).setFilter(true).setDictType(dict);
    }
    /*
     * 客户信息列表
     */
    public VueResult storeGuestList() throws Exception {
        boolean power=PowerUtil.hasPower(PowerUtil.Store);
        VueListResult result=getVueListResult("客户信息列表",GuestInfo.class,actionUrl("storeGuestList"));
        result.setFunction(LevelUtil.exportFunc(),false);
        result.setHeight(-1);
        result.addCol(toIndexColumn());
        result.addCol(getTypeColumn());
        result.addCol(toLinkColumn("客户名称", "name", 150, true,
            new ListEvent(actionUrl("toView"), EventType.MODAL,1000)
                .addParam("model.oid","oid")
        ));
        result.addCol(toTextColumn("负责人", "dutyUser", 80, true));
        result.addCol(toTextColumn("联系电话", "tel", 110, true));
        result.addCol(toTextColumn("客户地址", "address", 150, true));
        result.addCol(toTextColumn("描述", "description", 150, true));
        result.addCol(toNumberColumn("金额", "stockAmount", 80, true).setGroupTitle("入库-欠款"));
        result.addCol(toNumberColumn("已结", "stockPayAmount", 80, true).setGroupTitle("入库-欠款"));
        result.addCol(toNumberColumn("金额", "saleAmount", 80, true).setGroupTitle("出库-应收"));
        result.addCol(toNumberColumn("已结", "saleIncomeAmount", 80, true).setGroupTitle("出库-应收"));
        result.addCol(toNumberColumn("剩余金额", "surplusAmount", 100, true));
        result.addCol(toTextColumn("管理用户", "manageUser.realname", 100, true));
        result.addCol(toTextColumn("录入用户", "inputUser.realname", 100, true));
        result.addCol(toStatusColumn(power, true));
        result.addCol(toSpaceColumn("操作", 100,
            ListButton.modal("修改", ButtonType.PRIMARY,actionUrl("toEdit"),1000, SuccessMethod.REFRESH)
                .addParam("model.oid","oid")
        ).setFixed(Align.RIGHT));
        result.addButton(
            ListButton.modal("新增", ButtonType.PRIMARY,actionUrl("toEdit"),1000, SuccessMethod.REFRESH)
        );
        //排序 result.addOrder
        //查询条件 result.addCondition
        result.addCondition(QueryCondition.eq("store.oid",PowerUtil.getSessionStoreOid()));
        return result;
    }
    /*
     * 客户信息列表
     */
    public VueResult viewStoreGuestList() throws Exception {
        VueListResult result=getVueListResult("客户信息列表",GuestInfo.class,actionUrl("viewStoreGuestList"));
        result.setFunction(LevelUtil.exportFunc(),false);
        result.setHeight(-1);
        result.addQueryFields("inputUser.oid","manageUser.oid");
        result.addCol(toIndexColumn());
        result.addCol(getTypeColumn());
        result.addCol(toLinkColumn("客户名称", "name", 150, true,
            new ListEvent(actionUrl("toView"), EventType.MODAL,1300)
                .addParam("model.oid","oid")
        ));
        result.addCol(toTextColumn("负责人", "dutyUser", 80, true));
        result.addCol(toTextColumn("联系电话", "tel", 110, true));
        result.addCol(toTextColumn("客户地址", "address", 150, true));
        result.addCol(toTextColumn("描述", "description", 150, true));
        result.addCol(toNumberColumn("金额", "stockAmount", 80, true).setGroupTitle("入库-欠款"));
        result.addCol(toNumberColumn("已结", "stockPayAmount", 80, true).setGroupTitle("入库-欠款"));
        result.addCol(toNumberColumn("金额", "saleAmount", 80, true).setGroupTitle("出库-应收"));
        result.addCol(toNumberColumn("已结", "saleIncomeAmount", 80, true).setGroupTitle("出库-应收"));
        result.addCol(toNumberColumn("剩余金额", "surplusAmount", 100, true));
        result.addCol(toTextColumn("管理用户", "manageUser.realname", 100, true));
        result.addCol(toTextColumn("录入用户", "inputUser.realname", 100, true));
        result.addCol(toStatusColumn(false, true));
        result.addCol(toSpaceColumn("操作", 100,
            ListElement.test("record.inputUser.oid=='"+PowerUtil.getSessionUserOid()+"'||record.manageUser.oid=='"+PowerUtil.getSessionUserOid()+"'",
                ListButton.modal("修改", ButtonType.PRIMARY,actionUrl("toEdit"),1000, SuccessMethod.REFRESH)
                    .addParam("model.oid","oid")
            )
        ).setFixed(Align.RIGHT));
        result.addButton(
            ListButton.modal("新增", ButtonType.PRIMARY,actionUrl("toEdit"),1000, SuccessMethod.REFRESH)
        );
        //排序 result.addOrder
        //查询条件 result.addCondition
        result.addCondition(QueryCondition.eq("store.oid",PowerUtil.getSessionStoreOid()));
        return result;
    }
    /*
     * 客户信息列表
     */
    public VueResult selectOneGuest() throws Exception {
        String selected=getParamsValue("selected","");
        VueListResult result=getVueListResult("选择客户",GuestInfo.class,actionUrl("selectOneGuest"));
        result.setFunction(LevelUtil.exportFunc(),false);
        result.setHeight(-1);
        result.addQueryFields("oid");
        result.addCol(toIndexColumn());
        result.addCol(getTypeColumn());
        result.addCol(toTextColumn("客户名称", "name", 130, true));
        result.addCol(toTextColumn("负责人", "dutyUser", 80, true));
        result.addCol(toTextColumn("联系电话", "tel", 110, true));
        result.addCol(toTextColumn("客户地址", "address", 150, true));
        result.addCol(toTextColumn("描述", "description", 150, true));
        result.addCol(toNumberColumn("金额", "stockAmount", 80, true).setGroupTitle("入库-欠款"));
        result.addCol(toNumberColumn("付款", "stockPayAmount", 80, true).setGroupTitle("入库-欠款"));
        result.addCol(toNumberColumn("金额", "saleAmount", 80, true).setGroupTitle("出库-应收"));
        result.addCol(toNumberColumn("收款", "saleIncomeAmount", 80, true).setGroupTitle("出库-应收"));
        result.addCol(toNumberColumn("结余", "surplusAmount", 100, true));
        result.addCol(toTextColumn("管理用户", "manageUser.realname", 100, true));
        result.addCol(toTextColumn("录入用户", "inputUser.realname", 100, true));
        result.addCol(toSpaceColumn("操作", 100,
            ListElement.test("record.oid!='"+selected+"'",
                ListButton.back("选择", ButtonType.PRIMARY)
                    .addParam("","")
            ),
            ListElement.elseOther(ListElement.tag("已选",""))
        ).setFixed(Align.RIGHT));
        result.addButton(
            ListButton.modal("新增", ButtonType.PRIMARY,actionUrl("toEdit"),1000, SuccessMethod.REFRESH)
        );
        //排序 result.addOrder
        //查询条件 result.addCondition
        result.addCondition(QueryCondition.eq("store.oid",PowerUtil.getSessionStoreOid()));
        result.addCondition(QueryCondition.not(QueryCondition.eq("oid",PowerUtil.getSelfGuestInfo().getOid())));
        return result;
    }
    @Override
    public GuestInfo getModel() {
        return model;
    }

    @Override
    public String[] getStatusPower() {
        return new String[]{};
    }

    public void setModel(GuestInfo model) {
        this.model = model;
    }
}
